package com.copyurl.util;

import com.copyurl.common.SpringAnnotations;
import com.intellij.psi.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PsiElementUtil {
    public PsiElementUtil() {
    }

    private static List<String> getValues(PsiNameValuePair psiNameValuePair) {
        List<String> values = new ArrayList<>();
        PsiAnnotationMemberValue value = psiNameValuePair.getValue();
        if (value instanceof PsiReferenceExpression expression) {
            values.add(expression.getText());
        } else if (value instanceof PsiLiteralExpression) {
            values.add(psiNameValuePair.getLiteralValue());
        } else if (value instanceof PsiArrayInitializerMemberValue arrayValue) {
            PsiAnnotationMemberValue[] var4 = arrayValue.getInitializers();
            for (PsiAnnotationMemberValue initializer : var4) {
                values.add(initializer.getText().replaceAll("\"", ""));
            }
        }

        return values;
    }

    private static List<String> getAttributeValue(PsiNameValuePair[] attributes, String attributeName) {
        List<String> values = new ArrayList<>();
        values.add("");
        if (attributes != null && attributes.length == 1) {
            PsiNameValuePair psiNameValuePair = attributes[0];
            if (psiNameValuePair.getName() != null
                    && psiNameValuePair.getName().equalsIgnoreCase(attributeName)
                    || psiNameValuePair.getName() == null
                    && ("value".equalsIgnoreCase(attributeName)
                    || "path".equalsIgnoreCase(attributeName))) {
                return getValues(psiNameValuePair);
            }
        } else if (attributes != null && attributes.length > 1) {
            Optional<PsiNameValuePair> psiNameValuePair = Stream.of(attributes).filter((a) -> a.getName() != null && a.getName().equalsIgnoreCase(attributeName)).findFirst();
            if (psiNameValuePair.isPresent()) {
                return getValues(psiNameValuePair.get());
            }
        }

        return values;
    }

    public static String createQueryWithParameters(PsiParameterList parameterList) {
        StringBuilder query = new StringBuilder();
        List<String> params = new ArrayList<>();
        PsiParameter[] parameters = parameterList.getParameters();

        for (PsiParameter parameter : parameters) {
            PsiModifierList modifierList = parameter.getModifierList();
            if (SpringUtil.containsSpringAnnotation(SpringAnnotations.REQUEST_PARAM, modifierList)) {
                String paramValue = getAnnotationValue(modifierList, "value", SpringAnnotations.REQUEST_PARAM);
                if (paramValue.isEmpty()) {
                    params.add(parameter.getName());
                } else {
                    params.add(paramValue);
                }
            }
        }

        if (params.size() > 0) {
            query.append("?");
            query.append(params.stream().map((s) -> s + "=").collect(Collectors.joining("&")));
        }

        return query.toString();
    }

    public static String getAnnotationValue(PsiModifierList modifierList, String attributeName, SpringAnnotations springAnnotation) {
        List<String> values = new ArrayList<>();
        values.add("");
        if (modifierList != null) {
            PsiAnnotation[] annotations = modifierList.getAnnotations();
            int var6 = annotations.length;

            for (PsiAnnotation psiAnnotation : annotations) {
                String qualifiedName = psiAnnotation.getQualifiedName();
                if (qualifiedName == null) {
                    continue;
                }
                if (qualifiedName.equalsIgnoreCase(springAnnotation.getQualifiedName())) {
                    PsiAnnotationParameterList parameterList = psiAnnotation.getParameterList();
                    PsiNameValuePair[] attributes = parameterList.getAttributes();
                    return getAttributeValue(attributes, attributeName).get(0);
                }
            }
        }

        return values.get(0);
    }

    public static String getUrl(PsiModifierList modifierList, SpringAnnotations springAnnotation) {
        String withPath = getAnnotationValue(modifierList, "path", springAnnotation);
        String withValue = getAnnotationValue(modifierList, "value", springAnnotation);
        if (withPath.isEmpty() && !withValue.isEmpty()) {
            return withValue;
        } else {
            return !withPath.isEmpty() && withValue.isEmpty() ? withPath : withValue;
        }
    }
}
